package com.amose.model4.controller;

import com.amose.model4.entity.Article;
import com.amose.model4.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;

/**
 * @author yangpeng
 * @time 2020/3/24
 * @since 1.0.0
 */
@Controller
public class BlogController {

    @Autowired
    ArticleService articleService;

    @RequestMapping("/")
    public String index() {
        return "redirect:/list";
    }

    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "pageNum", defaultValue = "0") int pageNum, @RequestParam(value = "pageSize", defaultValue = "2") int pageSize) {
        Page<Article> articles=articleService.queryPage(pageNum, pageSize);
        System.out.println("总页数" + articles.getTotalPages());
        System.out.println("当前页是：" + pageNum);

        System.out.println("分页数据：");
        Iterator<Article> articleIterator = articles.iterator();
        while (articleIterator.hasNext()){
            System.out.println(articleIterator.next().toString());
        }
        model.addAttribute("articles", articles);
        return "index";
    }
}
